<?php

namespace App\Http\Controllers;

use App\Framework\Session\ManagerSession;
use App\Http\Requests\Admin\GetManagerHandleLogForm;
use App\Http\Requests\Admin\GetManagerListForm;
use App\Http\Requests\Admin\GetManagerLoginLogForm;
use App\Http\Requests\Admin\ManagerLoginForm;
use App\Http\Requests\Admin\StoreManagerForm;
use App\Http\Requests\Admin\StoreManagerRoleForm;
use App\Http\Requests\Admin\storePermissionForm;
use App\Http\Requests\Admin\UpdateManagerForm;
use App\Http\Requests\Admin\UpdateManagerRoleForm;
use Illuminate\Http\Request;
use AdminFacade;
use Illuminate\Support\Facades\URL;
use LogFacade;

class AdminController extends Controller
{
    //region 登录及首页
    //首页
    public function index()
    {
        $manager = ManagerSession::getCurrent();
        return view('admin.index')->with('manager', $manager);
    }

    //内嵌首页
    public function innerIndex()
    {
        return view('admin.innerIndex');
    }

    //登录页面
    public function login(Request $request)
    {
        return view('admin.login');
    }

    //提交登录信息
    public function postLogin(ManagerLoginForm $form)
    {
        $message = AdminFacade::login($form);
        if ($message->getSuccess()) {
            return redirect(URL::action('AdminController@index'));
        }

        return $this->showMessageAndBack($message->getInfo());
    }

    //登出
    public function logout()
    {
        AdminFacade::logout();
        return $this->showMessageAndRedirect('您已安全退出系统！', URL::action('AdminController@login'));
    }

    //临时提示页面
    public function todo()
    {
        echo '<h1>to do</h1>';
    }

    //endregion

    //region 管理员角色

    //角色列表
    public function getManagerRoleList()
    {
        $this->storeAdminLog('查询管理员角色');
        $roles = AdminFacade::getAllRole();
        return view('admin.getManagerRoleList')->with('roles', $roles);
    }

    //添加管理员角色
    public function addManagerRole()
    {
        $this->storeAdminLog('添加管理员角色');
        return view('admin.addManagerRole');
    }

    //添加管理员角色
    public function storeManagerRole(StoreManagerRoleForm $form)
    {
        $this->storeAdminLog('保存添加的管理员角色');
        AdminFacade::addRole($form);
        return $this->showMessageAndRedirect('添加成功', URL::action('AdminController@getManagerRoleList'));
    }

    //编辑角色
    public function editManagerRole($id)
    {
        $this->storeAdminLog('编辑管理员角色');
        $role = AdminFacade::getRole($id);
        return view('admin.editManagerRole')->with('role', $role);
    }

    //修改角色
    public function updateManagerRole(UpdateManagerRoleForm $form)
    {
        $this->storeAdminLog('保存编辑的管理员角色');
        $msg = AdminFacade::updateRole($form);
        if ($msg->fail) {
            return $this->showMessageAndBack($msg->info);
        }

        return $this->showMessageAndRedirect($msg->info,
            URL::action('AdminController@getManagerRoleList'));
    }

    //配置权限
    public function setPermission($id)
    {
        $this->storeAdminLog('配置角色权限');
        $role = AdminFacade::getRole($id);
        if (empty($role)) {
            return $this->showMessageAndBack('角色不存在');
        }

        $modules = AdminFacade::getModules();
        $permissions = AdminFacade::getModulePermissions();
        $rolePermissions = AdminFacade::getRolePermission($role->manager_role_id);
        //提取模块对应权限
        $modPermission = [];
        foreach ($rolePermissions as $rp) {
            $modPermission[$rp->manager_module_id] = $rp->permission_value;
        }

        return view('admin.setPermission')
            ->with('role', $role)
            ->with('modules', $modules)
            ->with('permissions', $permissions)
            ->with('rolePermissions', $rolePermissions)
            ->with('modPermission', $modPermission);
    }

    //保存权限
    public function storePermission(StorePermissionForm $form)
    {
        $this->storeAdminLog('保存配置的角色权限');
        AdminFacade::setPermission($form);
        return $this->showMessageAndRedirect('配置成功', URL::action('AdminController@getManagerRoleList'));
    }

    //endregion

    //region 管理员管理

    //管理员列表
    public function getManagerList(GetManagerListForm $form)
    {
        $this->storeAdminLog('查询管理员列表');
        $managers = AdminFacade::getManagerList($form);

        return view('admin.getManagerList')
            ->with('pagerSet', $managers)
            ->with('form', $form);
    }

    //添加管理员
    public function addManager()
    {
        $this->storeAdminLog('添加管理员');
        $roles = AdminFacade::getAllRole();
        return view('admin.addManager')
            ->with('roles', $roles);
    }

    //存储管理员
    public function storeManager(StoreManagerForm $form)
    {
        $this->storeAdminLog('保存添加的管理员');
        $msg = AdminFacade::addManager($form);
        if ($msg->fail) {
            return $this->showMessageAndBack($msg->info);
        }

        return $this->showMessageAndRedirect('添加成功', URL::action('AdminController@getManagerList'));
    }

    //编辑管理员
    public function editManager($id)
    {
        $this->storeAdminLog('编辑管理员');
        $manager = AdminFacade::getManager($id);
        $roles = AdminFacade::getAllRole();

        return view('admin.editManager')
            ->with('manager', $manager)
            ->with('roles', $roles);
    }

    //修改管理员
    public function updateManager(UpdateManagerForm $form)
    {
        $this->storeAdminLog('保存编辑的管理员');
        $msg = AdminFacade::updateManager($form);
        if ($msg->fail) {
            return $this->showMessageAndBack($msg->info);
        }

        return $this->showMessageAndRedirect($msg->info, URL::action('AdminController@getManagerList'));
    }

    //删除管理员
    public function deleteManager(Request $request)
    {
        $this->storeAdminLog('删除管理员');
        $id = $request->get('id');
        $msg = AdminFacade::deleteManager($id);
        return response()->json($msg);
    }

    //endregion

    //region 日志

    //查询管理员登录日志
    public function getManagerLoginLog(GetManagerLoginLogForm $form)
    {
        $logs = LogFacade::getLoginLog($form);

        return view('admin.getManagerLoginLog')
            ->with('pagerSet', $logs)
            ->with('form', $form);
    }

    //查询管理员登录日志
    public function getManagerHandleLog(GetManagerHandleLogForm $form)
    {
        $logs = LogFacade::getHandleLog($form);

        return view('admin.getManagerHandleLog')
            ->with('pagerSet', $logs)
            ->with('form', $form);
    }

    //endregion


}
